package com.ebelter.bodyfatscale.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.model.Birthday;
import com.ebelter.bodyfatscale.moudules.db.Daos;
import com.ebelter.bodyfatscale.moudules.db.beans.UsersData;
import com.ebelter.bodyfatscale.ui.view.BirthdayView;
import com.ebelter.bodyfatscale.util.NumUtils;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.bodyfatscale.util.UserSpUtil;
import com.ebelter.btlibrary.btble.impl.scale.model.ScaleUser;
import com.ebelter.btlibrary.util.ULog;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;

//修改身高界面
public class ModifyBirthdayActivity extends BaseActivity {


    private static final String TAG = "ModifyBirthdayActivity";
    public static final int REQUESTCODE = 333;

    public static String ORIGINAL_BIRTHDAY = "ORIGINAL_BIRTHDAY";



    @BindView(R.id.modifybirthday_back_bt)
    ImageView modifybirthday_back_bt;

    @BindView(R.id.bdv_birthday_pager)
    BirthdayView bdv_birthday_pager;

    @BindView(R.id.modifybirthday_done_tv)
    TextView modifybirthday_done_tv;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_modifybirthday;
    }

    @Override
    protected void initViews() {

    }

    Birthday originalBirthday;
    @Override
    protected void initData() {

        originalBirthday = (Birthday) getIntent().getSerializableExtra(ORIGINAL_BIRTHDAY);
        if (originalBirthday==null){
            finish();
        }

        bdv_birthday_pager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                bdv_birthday_pager.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                bdv_birthday_pager.setCurrentDate(originalBirthday.getYear(),originalBirthday.getMonth(),originalBirthday.getDay());
            }
        });


    }


    @OnClick({ R.id.modifybirthday_back_bt,R.id.modifybirthday_done_tv})
    public void btClick(View view){
        switch (view.getId()){
            case R.id.modifybirthday_back_bt:
                finish();
                break;
            case R.id.modifybirthday_done_tv:
                modifyBirthday();
                break;


        }
    }

    private void modifyBirthday() {
        String birthday=bdv_birthday_pager.getBirthday().getFatterDay();
        ULog.i(TAG,"---"+birthday);
        if (TextUtils.equals(birthday,originalBirthday.getFatterDay())){
            //说明相同就直接提示修改成功就好了
            ToastUtil.show(R.string.Modify_Success);
            finish();
        }else {


            long userid = UserSpUtil.readLong(Constants.IUser.USER_ID);
            UsersData usersData = Daos.getInstance().getUsersDataFromUserId(userid);
            usersData.birthday=birthday;
            usersData.update();
            ToastUtil.show(R.string.Modify_Success);


            //说明更改了身高
            UserSpUtil.writeString(Constants.IUser.BIRTHDAY, birthday);
            int year=getBirthdayIntValue((String) birthday,0,1990);
            UserSpUtil.writeInt(Constants.IUser.YEAR,year);

            originalBirthday.setYear(year);
            int mon=getBirthdayIntValue((String) birthday,1,6);
            UserSpUtil.writeInt(Constants.IUser.MONTHS,mon);

            originalBirthday.setMonth(mon);
            int day=getBirthdayIntValue((String) birthday,2,1);
            UserSpUtil.writeInt(Constants.IUser.DAY,day);

            originalBirthday.setDay(day);
            UserSpUtil.writeInt(Constants.IUser.AGE,getAgeByBirthday(year));
            if (!Constants.isGuestModel){
                ScaleUser.getUser().setAge(getAgeByBirthday(year));
            }






        }
    }





    private int getBirthdayIntValue(String string,int index,int defaultValue){
        if (TextUtils.isEmpty(string)){
            return defaultValue;
        }
        String[] args = string.split("-");
        if (args.length!=3){
            return defaultValue;
        }
        return NumUtils.string2Int(args[index],defaultValue);
    }
    private int getAgeByBirthday(int year){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR)-year;
    }
}
